home *** CD-ROM | disk | FTP | other *** search
/ The AGA Experience 3 / AGA Experience Volume 3 (1997)(NFA - SAdENESS)[!].iso / software / utilities / programmers / c_dt / dt_source / libfuncs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-03  |  10.9 KB  |  520 lines

  1. /*
  2. **      $VER: libfuncs.c 43.4 (3.1.97)
  3. **
  4. **      datatype functions
  5. **
  6. **      (C) Copyright 1996-97 Andreas R. Kleinert
  7. **      All Rights Reserved.
  8. */
  9.  
  10. #define __USE_SYSBASE
  11.  
  12. #ifndef EXEC_TYPES_H
  13. #include <exec/types.h>
  14. #endif /* EXEC_TYPES_H */
  15.  
  16. #ifndef EXEC_MEMORY_H
  17. #include <exec/memory.h>
  18. #endif /* EXEC_MEMORY_H */
  19.  
  20. #ifndef GRAPHICS_GFXBASE_H
  21. #include <graphics/gfxbase.h>
  22. #endif /* GRAPHICS_GFXBASE_H */
  23.  
  24. #ifndef GRAPHICS_VIEW_H
  25. #include <graphics/view.h>
  26. #endif /* GRAPHICS_VIEW_H */
  27.  
  28. #include <cybergraphics/cybergraphics.h>
  29.  
  30. #include <datatypes/pictureclass.h>
  31. #include <datatypes/PictureClassExt.h>
  32.  
  33. #include <proto/exec.h>
  34. #include <proto/dos.h>
  35. #include <proto/intuition.h>
  36. #include <proto/graphics.h>
  37. #include <proto/utility.h>
  38. #include <proto/datatypes.h>
  39.  
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43.  
  44. #include <class/classbase.h>
  45. #include "libfuncs.h"
  46.  
  47.  
  48. #define LibVer(x) ( ((struct Library *) x)->lib_Version )
  49.  
  50.  
  51. Class * __saveds __asm ObtainPicClass ( register __a6 struct ClassBase *cb)
  52. {
  53.  return (cb->cb_Class);
  54. }
  55.  
  56. ULONG setdtattrs (struct ClassBase * cb, Object * o, ULONG data,...)
  57. {
  58.  return (SetDTAttrsA (o, NULL, NULL, (struct TagItem *) & data));
  59. }
  60.  
  61. ULONG getdtattrs (struct ClassBase * cb, Object * o, ULONG data,...)
  62. {
  63.  return (GetDTAttrsA (o, (struct TagItem *) & data));
  64. }
  65.  
  66.  
  67. extern char __aligned ExLibName [];
  68.  
  69. Class *initClass (struct ClassBase * cb)
  70. {
  71.  Class *cl;
  72.  
  73.  if (cl = MakeClass (&ExLibName[0], PICTUREDTCLASS, NULL, NULL, 0L))
  74.   {
  75.    cl->cl_Dispatcher.h_Entry = (HOOKFUNC) Dispatch;
  76.    cl->cl_UserData = (ULONG) cb;
  77.    AddClass (cl);
  78.   }
  79.  
  80.  return (cl);
  81. }
  82.  
  83. ULONG __saveds __asm Dispatch ( register __a0 Class * cl, register __a2 Object * o, register __a1 Msg msg)
  84. {
  85.  struct ClassBase *cb = (struct ClassBase *) cl->cl_UserData;
  86.  ULONG retval;
  87.  
  88.  switch (msg->MethodID)
  89.   {
  90.    case OM_NEW:
  91.     {
  92.      if (retval = DoSuperMethodA (cl, o, msg))
  93.       {
  94.        if (!GetGfxData (cb, cl, (Object *) retval, ((struct opSet *) msg)->ops_AttrList))
  95.         {
  96.          CoerceMethod (cl, (Object *) retval, OM_DISPOSE);
  97.  
  98.      return(NULL);
  99.         }
  100.       }
  101.      break;
  102.     }
  103.    default:
  104.     {
  105.      retval = (ULONG) DoSuperMethodA (cl, o, msg);
  106.      break;
  107.     }
  108.   }
  109.  
  110.  return (retval);
  111. }
  112.  
  113. ULONG __saveds __stdargs DTS_ReadIntoBitMap(struct ClassBase *cb, Object * o, Class *cl, struct TagItem * attrs);
  114. ULONG __saveds __stdargs DTS_ReadIntoBitMap43(struct ClassBase *cb, Object * o, Class *cl, struct TagItem * attrs);
  115.  
  116. ULONG __saveds __asm GetGfxData ( register __a6 struct ClassBase * cb, register __a0 Class * cl, register __a2 Object * o, register __a1 struct TagItem * attrs)
  117. {
  118.  ULONG retval;
  119.  extern struct Library *SuperClassBase;
  120.  
  121.  
  122.  if(LibVer(SuperClassBase) > 42) retval = DTS_ReadIntoBitMap43(cb, o, cl, attrs);
  123.   else                           retval = DTS_ReadIntoBitMap(  cb, o, cl, attrs);
  124.  
  125.  return(retval);
  126. }
  127.  
  128. ULONG __saveds __stdargs DTS_GetBestModeID(ULONG width, ULONG height, ULONG depth);
  129. ULONG __saveds __stdargs DTS_SkipComment(BPTR handle, UBYTE *buf);
  130.  
  131. ULONG __saveds __stdargs DTS_ReadIntoBitMap(struct ClassBase *cb, Object * o, Class *cl, struct TagItem * attrs)
  132. {
  133.  struct BitMapHeader *bmhd;
  134.  
  135.  BOOL success = TRUE;
  136.  
  137.  struct RastPort __aligned trp;
  138.  struct RastPort __aligned rp;
  139.  
  140.  struct BitMap *bm, *tbm;
  141.  
  142.  ULONG i, width, height, depth, maxval;
  143.  
  144.  UBYTE *readname, *buffer, *id;
  145.  
  146.  struct ColorRegister *cmap;
  147.  LONG *cregs;
  148.  
  149.  BPTR fh;
  150.  
  151.  UBYTE wstr[16], hstr[16], dstr[16];
  152.  UWORD ID = (UWORD) 0;
  153.  
  154.  
  155.  readname = (UBYTE *) GetTagData (DTA_Name, NULL, attrs);
  156.  getdtattrs (cb, o, PDTA_BitMapHeader, &bmhd, TAG_DONE, NULL);
  157.  
  158.  
  159.  fh = Open(readname, MODE_OLDFILE);
  160.  if(!fh)
  161.   {
  162.    SetIoErr(ERROR_OBJECT_NOT_FOUND);
  163.    return(FALSE);
  164.   }
  165.  
  166.  FRead(fh, &ID, 2, 1);
  167.  FRead(fh, &wstr[0], 1, 1); /* filter CR */
  168.  
  169.  id = (UBYTE *) &ID;
  170.  if(id[1] != '5')
  171.   {
  172.    SetIoErr(ERROR_OBJECT_WRONG_TYPE);
  173.    Close(fh);
  174.    return(FALSE);
  175.   }
  176.  
  177.  
  178.  /* get Width */
  179.  
  180.  for(i=0; i<16; i++)
  181.   {
  182.    if(!FRead(fh, &wstr[i], 1, 1))
  183.     {
  184.      Close(fh);
  185.      return(FALSE);
  186.     }
  187.  
  188.    if(  wstr[i]=='#') i = DTS_SkipComment(fh, wstr);
  189.    if( (wstr[i]==' ') || (wstr[i]==0xa) || (wstr[i]==13) )
  190.     {
  191.      wstr[i] = (UBYTE) '\0';
  192.      break;
  193.     }
  194.   }
  195.  
  196.  /* get Height */
  197.  
  198.  for(i=0; i<16; i++)
  199.   {
  200.    if(!FRead(fh, &hstr[i], 1, 1))
  201.     {
  202.      Close(fh);
  203.      return(FALSE);
  204.     }
  205.  
  206.    if(  hstr[i]=='#') i = DTS_SkipComment(fh, hstr);
  207.    if( (hstr[i]==' ') || (hstr[i]==0xa) || (hstr[i]==13) )
  208.     {
  209.      hstr[i] = (UBYTE) '\0';
  210.      break;
  211.     }
  212.   }
  213.  
  214.  /* Maxval */
  215.  
  216.  for(i=0; i<16; i++)
  217.   {
  218.    if(!FRead(fh, &dstr[i], 1, 1))
  219.     {
  220.      Close(fh);
  221.      return(FALSE);
  222.     }
  223.  
  224.    if(  dstr[i]=='#') i = DTS_SkipComment(fh, dstr);
  225.    if( (dstr[i]==' ') || (dstr[i]==0xa) || (dstr[i]==13) )
  226.     {
  227.      dstr[i] = (UBYTE) '\0';
  228.      break;
  229.     }
  230.   }
  231.  
  232.  width  = (ULONG) atol(wstr);
  233.  height = (ULONG) atol(hstr);
  234.  maxval = (ULONG) atol(dstr);
  235.  if(maxval > 255)
  236.   {
  237.    Close(fh);
  238.    return(FALSE);
  239.   }
  240.  
  241.  bmhd->bmh_Width  = (bmhd->bmh_PageWidth  = width);
  242.  bmhd->bmh_Height = (bmhd->bmh_PageHeight = height);
  243.  depth            = (bmhd->bmh_Depth = 8);
  244.  
  245.  setdtattrs(cb, o, PDTA_NumColors,      256,
  246.                    TAG_DONE,            NULL);
  247.  
  248.  getdtattrs(cb, o, PDTA_ColorRegisters, (ULONG) &cmap,
  249.                    PDTA_CRegs,            &cregs,
  250.                    TAG_DONE,            NULL);
  251.  
  252.  if( (!cmap) || (!cregs) )
  253.   {
  254.    success = FALSE;
  255.   }else
  256.   {
  257.    if(tbm = AllocBitMap (bmhd->bmh_Width, 1, bmhd->bmh_Depth, BMF_CLEAR, NULL))
  258.     {
  259.      InitRastPort (&trp);
  260.      trp.BitMap = tbm;
  261.  
  262.      if (bm = AllocBitMap (bmhd->bmh_Width, bmhd->bmh_Height, bmhd->bmh_Depth, BMF_CLEAR, NULL))
  263.       {
  264.        InitRastPort (&rp);
  265.        rp.BitMap = bm;
  266.  
  267.        buffer = (APTR) AllocVec(width + 1024, MEMF_CLEAR|MEMF_PUBLIC);
  268.        if(buffer)
  269.         {
  270.          for(i=0; i<height; i++)
  271.           {
  272.            FRead(fh, buffer, width, 1);
  273.  
  274.            WritePixelLine8(&rp, 0, i, width, buffer, &trp);
  275.           }
  276.  
  277.          for(i=0; i<maxval; i++)
  278.           {
  279.            cmap->red   = i * (256 / maxval);
  280.            cmap->green = i * (256 / maxval);
  281.            cmap->blue  = i * (256 / maxval);
  282.            cmap++;
  283.  
  284.            cregs[i * 3    ] = ((LONG)i * (256 / maxval))<<24;
  285.            cregs[i * 3 + 1] = ((LONG)i * (256 / maxval))<<24;
  286.            cregs[i * 3 + 2] = ((LONG)i * (256 / maxval))<<24;
  287.           }
  288.  
  289.          cmap->red   = 255;
  290.          cmap->green = 255;
  291.          cmap->blue  = 255;
  292.          cmap++;
  293.  
  294.          cregs[i * 3    ] = ((LONG)255)<<24;
  295.          cregs[i * 3 + 1] = ((LONG)255)<<24;
  296.          cregs[i * 3 + 2] = ((LONG)255)<<24;
  297.  
  298.          setdtattrs (cb, o,
  299.                  DTA_ObjName,    readname,
  300.              DTA_NominalHoriz,    bmhd->bmh_Width,
  301.              DTA_NominalVert,    bmhd->bmh_Height,
  302.              PDTA_BitMap,    bm,
  303.              PDTA_ModeID,    DTS_GetBestModeID(bmhd->bmh_Width, bmhd->bmh_Height, 8),
  304.              TAG_DONE);
  305.  
  306.          FreeVec(buffer);
  307.         }else
  308.         {
  309.          FreeBitMap(bm);
  310.  
  311.          success = FALSE;
  312.          SetIoErr (ERROR_NO_FREE_STORE);
  313.         }
  314.  
  315.       }else
  316.       {
  317.        success = FALSE;
  318.        SetIoErr (ERROR_NO_FREE_STORE);
  319.       }
  320.  
  321.      FreeBitMap(tbm);
  322.     }else
  323.     {
  324.      success = FALSE;
  325.      SetIoErr (ERROR_NO_FREE_STORE);
  326.     }
  327.   }
  328.  
  329.  Close(fh);
  330.  
  331.  return(success);
  332. }
  333.  
  334. ULONG __saveds __stdargs DTS_ReadIntoBitMap43(struct ClassBase *cb, Object * o, Class *cl, struct TagItem * attrs)
  335. {
  336.  struct BitMapHeader *bmhd;
  337.  
  338.  BOOL success = TRUE;
  339.  
  340.  ULONG i, width, height, depth, maxval;
  341.  
  342.  UBYTE *readname, *buffer;
  343.  
  344.  BPTR fh;
  345.  
  346.  UBYTE wstr[16], hstr[16], dstr[16], *id;
  347.  UWORD ID;
  348.  
  349.  
  350.  readname = (UBYTE *) GetTagData (DTA_Name, NULL, attrs);
  351.  getdtattrs (cb, o, PDTA_BitMapHeader, &bmhd, TAG_DONE, NULL);
  352.  
  353.  
  354.  fh = Open(readname, MODE_OLDFILE);
  355.  if(!fh) return(FALSE);
  356.  
  357.  
  358.  FRead(fh, &ID, 2, 1);
  359.  FRead(fh, &wstr[0], 1, 1); /* filter CR */
  360.  
  361.  
  362.  id = (UBYTE *) &ID;
  363.  if(id[1] == '5')
  364.   {
  365.    /* for P5 with 256 grayscales, the V40 code does suffice */
  366.  
  367.    Close(fh);
  368.    return( DTS_ReadIntoBitMap(cb, o, cl, attrs) );
  369.   }
  370.  
  371.  /* get Width */
  372.  
  373.  for(i=0; i<16; i++)
  374.   {
  375.    if(!FRead(fh, &wstr[i], 1, 1))
  376.     {
  377.      Close(fh);
  378.      return(FALSE);
  379.     }
  380.  
  381.    if(  wstr[i]=='#') i = DTS_SkipComment(fh, wstr);
  382.    if( (wstr[i]==' ') || (wstr[i]==0xa) || (wstr[i]==13) )
  383.     {
  384.      wstr[i] = (UBYTE) '\0';
  385.      break;
  386.     }
  387.   }
  388.  
  389.  /* get Height */
  390.  
  391.  for(i=0; i<16; i++)
  392.   {
  393.    if(!FRead(fh, &hstr[i], 1, 1))
  394.     {
  395.      Close(fh);
  396.      return(FALSE);
  397.     }
  398.  
  399.    if(  hstr[i]=='#') i = DTS_SkipComment(fh, hstr);
  400.    if( (hstr[i]==' ') || (hstr[i]==0xa) || (hstr[i]==13) )
  401.     {
  402.      hstr[i] = (UBYTE) '\0';
  403.      break;
  404.     }
  405.   }
  406.  
  407.  /* Maxval */
  408.  
  409.  for(i=0; i<16; i++)
  410.   {
  411.    if(!FRead(fh, &dstr[i], 1, 1))
  412.     {
  413.      Close(fh);
  414.      return(FALSE);
  415.     }
  416.  
  417.    if(  dstr[i]=='#') i = DTS_SkipComment(fh, dstr);
  418.    if( (dstr[i]==' ') || (dstr[i]==0xa) || (dstr[i]==13) )
  419.     {
  420.      dstr[i] = (UBYTE) '\0';
  421.      break;
  422.     }
  423.   }
  424.  
  425.  width  = (ULONG) atol(wstr);
  426.  height = (ULONG) atol(hstr);
  427.  maxval = (ULONG) atol(dstr);
  428.  if(maxval > 255)
  429.   {
  430.    Close(fh);
  431.    return(FALSE);
  432.   }
  433.  
  434.  bmhd->bmh_Width  = (bmhd->bmh_PageWidth  = width);
  435.  bmhd->bmh_Height = (bmhd->bmh_PageHeight = height);
  436.  depth            = (bmhd->bmh_Depth = 24);
  437.  
  438.  setdtattrs(cb, o, 
  439.             DTA_ObjName,      readname,
  440.             DTA_NominalHoriz, bmhd->bmh_Width,
  441.             DTA_NominalVert,  bmhd->bmh_Height,
  442.             PDTA_SourceMode,  MODE_V43,
  443.             PDTA_ModeID,      DTS_GetBestModeID(bmhd->bmh_Width, bmhd->bmh_Height, 8),
  444.             TAG_DONE);
  445.  
  446.  
  447.  buffer = (APTR) AllocVec(width*3 + 1024, MEMF_CLEAR|MEMF_PUBLIC);
  448.  if(buffer)
  449.   {
  450.    ULONG i;
  451.  
  452.    for(i=0; i<height; i++)
  453.     {
  454.      FRead(fh, buffer, width*3, 1);
  455.      DoSuperMethod(cl, o, DTM_WRITEPIXELARRAY, buffer, RECTFMT_RGB, width*3, 0, i, width, 1);
  456.     }
  457.  
  458.    FreeVec(buffer);
  459.   }else
  460.   {
  461.    success = FALSE;
  462.    SetIoErr (ERROR_NO_FREE_STORE);
  463.   }
  464.  
  465.  Close(fh);
  466.  
  467.  return(success);
  468. }
  469.  
  470. ULONG __saveds __stdargs DTS_GetBestModeID(ULONG width, ULONG height, ULONG depth)
  471. {
  472.  ULONG mode_id = NULL;
  473.  
  474.  mode_id = BestModeID(BIDTAG_NominalWidth,  width,
  475.                       BIDTAG_NominalHeight, height,
  476.                       BIDTAG_DesiredWidth,  width,
  477.                       BIDTAG_DesiredHeight, height,
  478.                       BIDTAG_Depth,         depth,
  479.                       TAG_END);
  480.  
  481.  if(mode_id == INVALID_ID)
  482.   {
  483.    /* Uses OverScan values for checking. */
  484.    /* Assumes an ECS-System.             */
  485.  
  486.         if((width > 724) && (depth < 3)) mode_id = SUPER_KEY;
  487.    else if((width > 362) && (depth < 5)) mode_id = HIRES_KEY;
  488.    else                                  mode_id = LORES_KEY;
  489.  
  490.    if(!ModeNotAvailable(mode_id | PAL_MONITOR_ID))    /* for PAL  Systems */
  491.     {
  492.      if(height > 283) mode_id |= LACE;
  493.  
  494.      mode_id |= PAL_MONITOR_ID;
  495.     }else
  496.     {
  497.      if(!ModeNotAvailable(mode_id | NTSC_MONITOR_ID)) /* for NTSC Systems */
  498.       {
  499.        if(height > 241) mode_id |= LACE;
  500.  
  501.        mode_id |= NTSC_MONITOR_ID;
  502.       }
  503.     }
  504.   }
  505.  
  506.  return(mode_id);
  507. }
  508.  
  509. ULONG __saveds __stdargs DTS_SkipComment(BPTR handle, UBYTE *buf)
  510. {
  511.  do
  512.   {
  513.    FRead(handle, buf, 1, 1);
  514.   } while( (*buf!=0xa) && (*buf!=13) );
  515.  
  516.  FRead(handle, buf, 1, 1);
  517.  
  518.  return(0L);
  519. }
  520.